package com.meichis.mcsappframework.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.meichis.mcsappframework.R;
import com.meichis.mcsappframework.http.download.DownLoadCallBack;
import com.meichis.mcsappframework.http.download.DownManager;
import com.meichis.mcsappframework.utils.UniversalInterface;

import java.io.File;

public class DownloadDialog extends Dialog {

    private UniversalInterface<Void, File> complete;
    private String fileName, url, title;
    private TextView tv_title, tv_msg;
    private Button bt_confirm;
    private int retryCount;

    public DownloadDialog(Context context, String url, String fileName, UniversalInterface<Void, File> complete) {
        this(context, url, fileName, "文件下载", complete);
    }

    public DownloadDialog(Context context, String url, String fileName, String title, UniversalInterface<Void, File> complete) {
        super(context, R.style.Appupdatedialog);
        this.url = url;
        this.fileName = fileName;
        this.title = title;
        this.complete = complete;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_download);
        setCancelable(false);
        tv_title = findViewById(R.id.tv_title);
        tv_msg = findViewById(R.id.tv_msg);
        bt_confirm = findViewById(R.id.bt_confirm);
        bt_confirm.setText("后台下载");
        tv_title.setText(title);
        retryCount = 0;
        findViewById(R.id.bt_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownManager.getInstance().cancel(url);
                DownloadDialog.this.dismiss();
            }
        });
        findViewById(R.id.bt_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("后台下载".equals(bt_confirm.getText().toString())) {
                    DownloadDialog.this.hide();
                } else {
                    download();
                }
            }
        });
        download();
    }

    private void download() {
        DownManager.getInstance().download(url, getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), fileName, new DownLoadCallBack() {
            @Override
            public void inProgress(int progress, long total) {
                if (isShowing() && tv_msg != null) {
                    tv_msg.setText("文件下载中... " + progress + "%");
                }
            }

            @Override
            public void onResponse(File response) {
                DownloadDialog.this.dismiss();
                complete.todo(response);
            }

            @Override
            public void onError(Throwable e) {
                tv_msg.setText((retryCount == 0 ? "下载失败\n" : "下载失败" + "(" + retryCount + ")\n") + e.getMessage());
                bt_confirm.setText("重试");
                retryCount++;
            }

            @Override
            public void onCancel() {
                DownloadDialog.this.dismiss();
            }
        });
    }
}
